package finedev.work.vennue;

import java.io.IOException;
import java.util.HashMap;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class LoginActivity extends Activity {
	
	private Activity mActivity = null;
	
//	public static HashMap<String, String> mHashMapResult = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        
        mActivity = this;
        
        Button btn_login =  (Button) findViewById( R.id.login_btn_login );
        
        btn_login.setOnClickListener( new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ProgressDialog progressDialog = new ProgressDialog( mActivity );
				progressDialog.setMessage("Logging in...");
				progressDialog.setCancelable(false);
				
				LoginManager loginManager = new LoginManager( (LoginActivity) mActivity, progressDialog);
				
//				loginManager.execute( "http://www.vennue.net/new/m_login.php", 
//						((EditText) findViewById(R.id.login_edit_email)).getText().toString(),
//						((EditText) findViewById(R.id.login_edit_password)).getText().toString() );
				
				loginManager.execute( "http://www.vennue.net/new/m_login.php", 
						 String.valueOf("copper34"),
						 String.valueOf("woong2star") );
				
				Log.i("LoginActivity", "loginManager execute" );
				
				
			}
        });        
    }
    
    private void LoginSuccess()
    {
    	Log.i("LoginActivity", "LoginSuccess()" );
    	Intent intent = new Intent( mActivity, MainTabActivity.class );
    	startActivity( intent );
    }
    
    private void LoginFail()
    {
    	
    }
    
    protected void ReceiveSuccess(String responseStr) throws JsonParseException, JsonMappingException, IOException
    {
    	LoginManager.mHashMapResult = new ObjectMapper().readValue( responseStr, HashMap.class );
		Log.i("LoginActivity", "json = " + LoginManager.mHashMapResult.toString() );
		Log.i("LoginActivity", String.valueOf( LoginManager.mHashMapResult.get("result") ) );
		
		if ( String.valueOf( LoginManager.mHashMapResult.get("result") ).equals("true") ) {
			LoginSuccess();
		} else {
			LoginFail();
		}
    }
    protected void ReceiveFail(String errorMessage)
    {
    	
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_login, menu);
        return true;
    }
}
